//
//  CommunityCV.m
//  Hand-held_kitchen
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//


#import "ZongTouwenjian.pch"

#import "CommunityCV.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import "SheQuCollectionViewCell.h"
#import "SQ_Yi_Cell.h"
#import "SQ_Er_Cell.h"
#import "SQ_San_Cell.h"
#import "SheQumodel.h"
#import "BaseModel.h"
#import "shequ_collectionMoble.h"
#import "ZuiXinWebView.h"
#import "shequSanCellmobel.h"
#import "DArenVC.h"
#import "DArencollectionVC.h"
#import "JingPinCell.h"
#import "jingpinColectionVC.h"
#import "ChangChuVC.h"
#import "ChangChuCoolection.h"
#import "SheQuCollctionWangZhi.h"
#import "ChangChuCoolection.h"
//#import "lunbo.h"



@interface CommunityCV ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, tiaotiaoDelegate,tiaotiao2Delegate,tiaotiao3Deletage>
@property(nonatomic, retain)UISegmentedControl *seg;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)UICollectionView *collection;
@property(nonatomic,retain)NSMutableArray *lunboArr; // 轮播数组
@property(nonatomic,retain)NSMutableArray *diyiArr; // 第一个cell
@property(nonatomic,retain)NSMutableArray *dierArr; // 第一二个cell
@property(nonatomic,retain)NSMutableArray *disanArr; // 第三个cell
@property(nonatomic,retain)NSMutableArray *disanbanArr; // 第三个cell
@property(nonatomic,retain)NSMutableArray *disiArr;//
@property(nonatomic,retain)NSMutableArray *zuixinArr;// 最新数据数组
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIView *daohangView; // 导航View

@end

@implementation CommunityCV

-(void)dealloc
{
    [_seg release];
    [_tableView release];
    [_collection release];
    [_lunboArr release];
    [_diyiArr release];
    [_dierArr release];
    [_disanArr release];
    [_disanbanArr release];
    [_zuixinArr release];
    [_scrollView release];
    [_daohangView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.daohangView = [[UIView alloc]initWithFrame:CGRectMake(0, 137.5*GAO, 100 * KUAN, 30 *GAO)];
    // table 数据
    [self tableData];
    // collection
    [self collectionData];
    
    
    
    // UISCROLLVIE
    [self UIScrollVie];
    // uitable
    [self tablev];
    // collection
    [self KELAISHENVIEW];
    // seg
    [self creatbutton];
    // 加rootView
    self.navigationItem.titleView = _daohangView;
    [self shuaxin];

    
    
    
}
#pragma mark - 下拉刷新
- (void)shuaxin
{
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(),^{
                           
                           // 刷新
                           [self collectionData];
                           [self tableData];
                           //新版本是 MJ_header
                           [self.tableView.mj_header endRefreshing];
                       });
    }];
    //设置刷新控件
    self.tableView.mj_header = header;
    //设置文字
    header.arrowView.image = [UIImage imageNamed:@"jingxuan@3x"];
    //    [header setTitle:@"使劲拉!" forState:MJRefreshStateIdle];
    //    [header setTitle:@"松手刷!" forState:MJRefreshStatePulling];
    //    [header setTitle:@"等！！！" forState:MJRefreshStateRefreshing];
    header.stateLabel.font = [UIFont systemFontOfSize:12];
    
}

-(void)tableData
{
    NSString *str = @"http://api.izhangchu.com/?token=35DE343B5FF57DD91FB14EA5B2379AB1&user_id=1272909&methodName=ShequRecommend&version=4.3&";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 轮播数据
        NSArray *arr1 = responseObject[@"data"][@"activity"];
        self.lunboArr = [SheQumodel modelHanderWithArray:arr1];
        NSMutableArray *aaarr = [NSMutableArray array];
        // 轮播图
        for (SheQumodel *m in _lunboArr) {
            NSString *str = m.image;
            [aaarr addObject:str];
        }
        lunbo *b = [[lunbo alloc] initWithFrame:CGRectMake(0, 0, Width, 130 * GAO) stringArray:aaarr];
        _tableView.tableHeaderView = b;
        
        
        
        
        // 第一个cell数据
        NSArray *arr2 = responseObject[@"data"][@"shequ_talent"];
        self.diyiArr = [SheQumodel modelHanderWithArray:arr2];
        // 第二个cell数据
        NSArray *arr3 = responseObject[@"data"][@"shequ_marrow"];
        self.dierArr = [SheQumodel modelHanderWithArray:arr3];
        
        // 第三个cell数据
        NSArray *arr4 = responseObject[@"data"][@"shequ_topics"];
        
        self.disanbanArr = [NSMutableArray array];
        self.disanArr = [SheQumodel modelHanderWithArray:arr4];
        /**<  需要穿给第三个Cell里面的数据 */
        for (NSDictionary *dic in arr4) {
            // 取到data这个数组
            NSArray *iamgeArray = [dic objectForKey:@"data"];
            // moble初始化
            shequSanCellmobel *model = [[shequSanCellmobel alloc]init];
            // 初始化moble 数组
            model.imageArray = [NSMutableArray array];
            // 便利data数组
            for (NSDictionary *dic2 in iamgeArray) {
                // 把image这个key放进 moble.imaArr这个数组里
                [model.imageArray addObject:[dic2 objectForKey:@"image"]];
                
            }
          //  moble 添加到数组   
        [self.disanbanArr addObject:model];
           
        }
        /**<  需要穿给第三个Cell里面的数据 */
        /**<  网址 */
        self.disiArr  =[NSMutableArray array];

        for (NSDictionary *dic11 in arr4) {
            NSArray *iamgeArray33 = [dic11 objectForKey:@"data"];
            // 图片
            SheQuCollctionWangZhi *model = [[SheQuCollctionWangZhi alloc]init];
            model.imageArray = [NSMutableArray array];
            for (NSDictionary *dic22 in iamgeArray33) {
                [model.imageArray addObject:[dic22 objectForKey:@"id"]];
            }
            [self.disiArr addObject:model];
           
            
        }
        


        
       
        
        [_tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

    
    
    
}

// uiscrollView

-(void)tablev
{
    self.tableView =[[ UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
     _tableView.contentInset = UIEdgeInsetsMake(0, 0, 90* GAO, 0);
    [self.scrollView addSubview:_tableView];
    
    [_tableView registerClass:[SQ_Yi_Cell class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[SQ_Er_Cell class] forCellReuseIdentifier:@"cell2"];
    [_tableView registerClass:[SQ_San_Cell class] forCellReuseIdentifier:@"cell3"];
    
}
// table个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _disanArr.count;
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        /**<  第一个Cell */
        SQ_Yi_Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
//        SheQumodel *m  = _diyiArr[indexPath.row];
        /**<  传过去的数据 */
        cell1.arr = _diyiArr;
        /**<  点击方法签的协议 */
        cell1.delegate = self;
        return cell1;
    }else if (indexPath.row == 1){
        /**<  第二个Xell */
        SQ_Er_Cell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell2.deledate2 = self;
        cell2.arr3 = _dierArr;
        return cell2;
    }else{
        /**<  第三个Cell */
        SQ_San_Cell *cell3 = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        SheQumodel *m  = _disanArr[indexPath.row];
        cell3.delagate = self;
//        cell3.arr1 = _disanArr;
        /**<  需要穿过的monle */
        shequSanCellmobel * m1 = _disanbanArr[indexPath.row];
        cell3.moble =  m1;
/**<  aaa */
        SheQuCollctionWangZhi *m2 =_disiArr[indexPath.row];

        cell3.wangzhi = m2;
        
        cell3.zi.text = m.topic_name;
        return cell3;

    }
    
    
    
   
}
// table 返回高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        return 210 *GAO;
    }if (indexPath.row == 1) {
        
        return 180 * GAO;
    }else
    {
        return 180 * GAO;
    }
    
}
/**<  点击方法 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        DArenVC *daren = [[DArenVC alloc]init];
        [self.navigationController pushViewController:daren animated:YES];
        [daren release];
    }else if (indexPath.row == 1){
        JingPinCell *dieryr = [[JingPinCell alloc]init];
        [self.navigationController pushViewController:dieryr animated:YES];
        [dieryr release];
    }else{
        ChangChuVC *dierye = [[ChangChuVC alloc]init];
        SheQumodel *m  = _disanArr[indexPath.row];
        dierye.str =m.id1;
        
        [self.navigationController pushViewController:dierye animated:YES];
   
        [dierye release];
    }
    
}
/**<  协议第六布 代替cell里coll的点击方法 */
- (void)tiaotiao:(NSString *)idd
{
    DArencollectionVC *daren = [[DArencollectionVC alloc] init];
    daren.str = idd ;
    [self.navigationController pushViewController:daren animated:YES];
    [daren release];
}

- (void)tiaotiao2:(NSString *)id2
{
    jingpinColectionVC *dierye = [[jingpinColectionVC alloc]init];
    dierye.str = id2;
    [self.navigationController pushViewController:dierye animated:YES];
    [dierye release];
   
}

- (void)tiaotiao3:(NSString *)idd
{
    ChangChuCoolection *dierye = [[ChangChuCoolection alloc]init];
    dierye.str = idd;
    
    [self.navigationController pushViewController:dierye animated:YES];
    [dierye release];
    
}



/**<  coll嘚数据 */
-(void)collectionData
{
        NSString *str = @"http://api.izhangchu.com/?methodName=ShequList&size=20&is_marrow=0&page=1&token=35DE343B5FF57DD91FB14EA5B2379AB1&user_id=1272909&version=4.3&";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"data"];
        self.zuixinArr = [shequ_collectionMoble modelHanderWithArray:arr];
        
        
        [_collection reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}
/**<  coll */
-(void)KELAISHENVIEW
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // cell大小
    layout.itemSize = CGSizeMake((Width -3)/2, 249 * GAO);
    // 行列间距
    layout.minimumLineSpacing = 10 * KUAN;
    layout.minimumInteritemSpacing = 1 * GAO;
    // 边界
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 滚动方向
    // 创建过程类似tableView
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(375 * KUAN, 0, Width, Height) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.delegate = self;
    _collection.dataSource = self;
    [self.scrollView addSubview:_collection];
    [_collection registerClass:[SheQuCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];

    
}
/**<  coll的tiem*/
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _zuixinArr.count;
}
/**<  Cell*/
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SheQuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    shequ_collectionMoble *m = _zuixinArr[indexPath.item];
    
    [cell.tupian sd_setImageWithURL:[NSURL URLWithString:m.image]];
    [cell.touxiang sd_setImageWithURL:[NSURL URLWithString:m.head_img]];
    cell.nicheng.text = m.nick;
    cell.shijian.text = m.create_time;
    
    
    return cell;
}
// collection 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZuiXinWebView *dierye = [[ZuiXinWebView alloc]init];
    shequ_collectionMoble *m = _zuixinArr[indexPath.row];
    /**<  传过去的网址 */
    dierye.str =m.share_url;
    
    [self.navigationController pushViewController:dierye animated:YES];
}
















// seg
-(void)creatbutton
{
    self.seg = [[ UISegmentedControl alloc] initWithItems:@[@"推荐", @"最新"]];
    self.seg.backgroundColor = [ UIColor whiteColor];
    self.seg.frame = CGRectMake(0,0, 100 * KUAN, 30 *GAO);
    //segcolor.tintColor =[ UIColor clearColor];
    [self.seg addTarget:self action:@selector(seg:) forControlEvents:UIControlEventValueChanged];
    [_daohangView addSubview:self.seg];
    self.seg.selectedSegmentIndex = 0;
    [_seg release];
    
}


/**<  scrollView */
-(void)UIScrollVie
{
    // 1.创建
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
    // 2.设置
    
    _scrollView.backgroundColor = [UIColor yellowColor];
    // 3.添加父视图
    [self.view addSubview:_scrollView];
    // 4.内存
    [_scrollView release];
    _scrollView.delegate = self;
    /**< contentSize 内容大小 */
    _scrollView.contentSize = CGSizeMake(Width * 2, 0);
    /**< pagingEnbaled 整页翻动 */
    _scrollView.pagingEnabled = YES;
    
}



/**<  偏移量 */
-(void)seg:(UISegmentedControl *)s
{
    if (s.selectedSegmentIndex == 0) {
        
        _scrollView.contentOffset = CGPointMake(Width*0, 0);
    }else
    {
        _scrollView.contentOffset = CGPointMake(Width*1, 0);
        
    }
    
    
}



// 偏移量
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.seg.selectedSegmentIndex = _scrollView.contentOffset.x /Width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
